// pages/todayStop/todayStop.js
let app = getApp();
let util = require("../../utils/util");
import { getParkOrderStatisticsApi,getCarOrderListApi } from "../../api/index";
import { formatDate } from "../../utils/formatDate.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight-100,
    pageFlag: false,
    pageNum: 1,
    pageSize: 5,
    type:null,
    startTime:null,
    endTime:null,
    stopCarList: [],
    dayList: [],
    dayOrderStatistics: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.type === "day") {
      const { startTime, endTime } = util.getDayStartAndEndTimestamp();
      this.setData({
        startTime,
        endTime
      });
    } else {
      const { startTime, endTime } = util.getFirstAndLastDay();
      console.log(endTime);
      this.setData({
        startTime,
        endTime
      });
    }
    let parkId = wx.getStorageSync("parkId") ? wx.getStorageSync("parkId") : "";
    this.setData({ parkId, type: options.type });
    this.getParkOrderStatistics();
    this.getOrderDetail();
  },

  changeDate(e) {
    this.setData({
      startTime: e.detail.startTimestamp,
      endTime: e.detail.endTimestamp,
      pageNum: 1,
    });
    this.getParkOrderStatistics();
    this.getOrderDetail();
  },
  //每日订单统计
  async getParkOrderStatistics() {
    const { parkId,startTime,endTime } = this.data;
    const { data: res } = await getParkOrderStatisticsApi({
      parkId,
      startTime,
      endTime,
    });
    let dayList = [res.data];
    dayList.forEach((item) => {
      item.startTime = formatDate(item.startTime);
      item.endTime = formatDate(item.endTime);
      item.leftValue = item.monthInCount;
      item.rightValue = item.monthOutCount;
    });
    this.setData({
      dayList
    });
  },
  //   车费明细
  async getOrderDetail(next) {
    const { parkId, pageNum, pageSize, stopCarList,startTime, endTime  } = this.data;
    const { data: res } = await getCarOrderListApi({
      parkId,
      pageNum,
      pageSize,
      startTime,
      endTime,
    });
    const newStopCarList = next
      ? stopCarList.concat(res.data.records)
      : res.data.records;
    this.setData({
      stopCarList: newStopCarList,
      pageFlag: !res.data.records.length && next,
    });
  },

  // 触底事件函数
  scrollToLower() {
    const { pageFlag,stopCarList } = this.data;
    if (stopCarList.length === 0) {
      return;
    }
    if (!pageFlag) {
      this.setData({ pageNum: this.data.pageNum + 1 });
      this.getOrderDetail(true);
    }
  },
});
